﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using System.Data;
using System.Data.SqlClient;
using BaHuan.Entities;

namespace BaHuan.DataLayer
{
    public class LoaiPhieuDAO:BaseDAO
    {
        // save into DB, @id = -1: insert else, update
        public ResultObj SaveLoaiPhieu(LoaiPhieu loaiphieu)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spAddLoaiPhieu", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spAddLoaiPhieu";
            SqlParameter param = new SqlParameter();
            param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, loaiphieu.Id);
            cmd.Parameters.Add(param);
            param = CreateParameter("@Ten", DbType.String, ParameterDirection.Input, loaiphieu.Ten);
            cmd.Parameters.Add(param);
            param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
            cmd.Parameters.Add(param);

            conObj.Open();
            cmd.ExecuteNonQuery();
            conObj.Close();

            result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
            return result;
        }

        // Get all Loai Phieu in DB
        public ResultObj GetAllLoaiPhieu()
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<LoaiPhieu> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllLoaiPhieu", CommandType.StoredProcedure);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<LoaiPhieu>();
                LoaiPhieu item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new LoaiPhieu();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Ten = Convert.ToString(reader["Ten"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Get all NhaCungCap in DB with condition
        public ResultObj GetAllLoaiPhieuByID(LoaiPhieu loaiphieu)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            List<LoaiPhieu> lst = null;
            SqlCommand cmd = CreateSqlCommand("spGetAllLoaiPhieuByID", CommandType.StoredProcedure);
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, loaiphieu.Id);
            cmd.Parameters.Add(param);
            cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<LoaiPhieu>();
                LoaiPhieu item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new LoaiPhieu();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.Ten = Convert.ToString(reader["Ten"]);
                    lst.Add(item);
                }
            }
            result.Result = lst;
            result.ReturnCode = 1;

            return result;
        }

        // Delete a LoaiPhieu out of DB with condition
        public ResultObj DeleteLoaiPhieu(LoaiPhieu loaiphieu)
        {
            ResultObj result = new ResultObj();
            result.Result = null;
            SqlConnection conObj = DataConnection.GetSqlConnection();
            SqlCommand cmd = CreateSqlCommand("spDeleteLoaiPhieu", CommandType.StoredProcedure);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "spDeleteLoaiPhieu";
            SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, loaiphieu.Id);
            cmd.Parameters.Add(param);
            conObj.Open();
            result.ReturnCode = cmd.ExecuteNonQuery();
            conObj.Close();

            return result;
        }
    }
}
